﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package GameMode {
    import flash.events.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import Modules.franchise.data.*;
    import Modules.guide.ui.*;
    import Engine.*;
    import Modules.stats.types.*;

    public class GMPlaceMapResource extends GMObjectMove {

        protected var m_item:String;
        private var m_itemClass:Class;
        protected var m_resource:MapResource;
        private var m_constructionSite:ConstructionSite = null;
        public var t_headquarter:Headquarter;
        private var itemDef:Item;
        private var m_itemData:Object;
        private var m_needRoadTickerMessage:String;

        public function GMPlaceMapResource(_arg1:String, _arg2:Class, _arg3:Boolean=false){
            this.m_item = _arg1;
            this.m_itemClass = ((ItemClassDefinitions.getClassByItemName(_arg1)) || (_arg2));
            m_isGift = _arg3;
            this.itemDef = Global.gameSettings.getItemByName(this.m_item);
            if (this.itemDef.construction != null){
                this.m_constructionSite = ConstructionSite.createConstructionSite(this.m_item, this.m_itemClass);
            };
            if (this.itemDef.placeableOn == MapResource.PLACE_ON_ROAD_OR_NEARROAD){
                m_canPlaceOnRoadOrNearRoad = true;
            };
            if (this.itemDef.canPlaceToFriendHome == "true"){
                m_canPlaceToFriendHome = true;
            };
            this.m_needRoadTickerMessage = ZLoc.t("Main", "TickerBadBuildingRoadPlacement");
            if (this.itemDef.className == "Peddler"){
                this.m_needRoadTickerMessage = ZLoc.t("Main", "TickerBadPeddlerBuildingRoadPlacement");
            };
            super(null);
        }
        public function get itemName():String{
            return (this.m_item);
        }
        override protected function replaceResource(_arg1:MapResource):void{
            if (_arg1 != null){
                this.m_resource = _arg1;
            };
        }
        override public function onMouseMove(_arg1:MouseEvent):Boolean{
            var _local2:Boolean;
            if (!m_selectedObject){
                m_highlightedPoint = getMouseTilePos();
                this.m_resource = new this.m_itemClass(this.m_item);
                this.m_resource.setOuter(Global.world);
                if (this.m_constructionSite){
                    m_selectedObject = this.m_constructionSite;
                    m_selectedObject.setVisible(false);
                    m_selectedObject.addChild(this.m_resource);
                    this.m_resource.setCollisionFlags(Constants.COLLISION_NONE);
                    this.m_resource.lock();
                } else {
                    m_selectedObject = this.m_resource;
                };
                resetOnPlaceCollisions();
                registerAllOnPlaceCollisions();
                snapPoint(m_highlightedPoint);
                m_selectedObject.setPosition(m_highlightedPoint.x, m_highlightedPoint.y);
                m_objectDragOffset = new Point(0, ((-(m_selectedObject.getSize().y) * 10) * (GlobalEngine.viewport.getZoom() * 0.25)));
            };
            if (Global.franchiseManager.placementMode){
                this.m_resource.alpha = 1;
            } else {
                this.m_resource.alpha = 0.5;
            };
            Global.world.citySim.roadManager.updateResource(this.m_resource);
            m_autoRotate = ((this.m_resource.autoRotate) && ((m_selectedObject as MapResource).autoRotate));
            if (m_autoRotate){
                rotateToRoad(this.m_resource);
            };
            _local2 = super.onMouseMove(_arg1);
            return (_local2);
        }
        override public function disableMode():void{
            UI.popupUnlock();
            if (this.m_constructionSite){
                if (m_selectedObject){
                    m_selectedObject.removeChild(this.m_resource);
                    this.m_resource.detach();
                    this.m_resource.cleanUp();
                    m_selectedObject.setVisible(false);
                };
            };
            super.disableMode();
        }
        override public function enableMode():void{
            super.enableMode();
            UI.popupLock();
        }
        override protected function updateHighlightPoint(_arg1:MouseEvent):Boolean{
            var _local2:Boolean;
            if (Global.world.isHome()){
                return (super.updateHighlightPoint(_arg1));
            };
            if (m_canPlaceToFriendHome){
                return (super.updateHighlightPoint(_arg1));
            };
            super.updateHighlightPoint(_arg1);
            _local2 = Boolean(this.getLotSiteUnderGameObject(m_selectedObject));
            return (_local2);
        }
        protected function getLotSiteUnderGameObject(_arg1:GameObject):LotSite{
            var _local2:LotSite;
            var _local4:LotSite;
            var _local5:Vector3;
            var _local6:Vector3;
            var _local7:Number;
            var _local8:Number;
            var _local9:Boolean;
            var _local10:Vector3;
            var _local11:Vector3;
            var _local12:Number;
            var _local13:Number;
            var _local14:Boolean;
            var _local15:Boolean;
            var _local16:Boolean;
            var _local3:Array = Global.world.getObjectsByTypes([WorldObjectTypes.LOT_SITE]);
            for each (_local4 in _local3) {
                _local5 = _local4.getSize();
                _local6 = _arg1.getSize();
                _local7 = (_local5.x - _local6.x);
                _local8 = (_local5.y - _local6.y);
                _local9 = Boolean((((_local7 >= 0)) && ((_local8 >= 0))));
                if (_local9){
                    _local10 = _local4.getPosition();
                    _local11 = _arg1.getPosition();
                    _local12 = (_local10.x - _local11.x);
                    _local13 = (_local10.y - _local11.y);
                    _local14 = Boolean((((_local12 >= -(_local7))) && ((_local12 <= 0))));
                    _local15 = Boolean((((_local13 >= -(_local8))) && ((_local13 <= 0))));
                    _local16 = Boolean(((_local14) && (_local15)));
                    if (_local16){
                        _local2 = _local4;
                        break;
                    };
                };
            };
            return (_local2);
        }
        override protected function handleObjectDrop(_arg1:MouseEvent=null):void{
            var _local2:LotSite;
            var _local3:int;
            var _local4:GuideTile;
            var _local5:Vector2;
            var _local6:Vector3;
            if (((Global.isVisiting()) && (!((this.m_resource is Business))))){
                return;
            };
            if (((this.m_canPlaceOnRoadOrNearRoad) && ((false == this.m_isNearRoad)))){
                return;
            };
            if (!(this.m_resource is LotSite)){
                _local2 = this.getLotSiteUnderGameObject(m_selectedObject);
            };
            if (((Global.guide.isActive()) && (Global.guide.guideTiles.length))){
                m_objectPlacedSuccess = false;
                m_positionValid = (_arg1) ? this.updateHighlightPoint(_arg1) : true;
                _local3 = 0;
                while (_local3 < Global.guide.guideTiles.length) {
                    _local4 = (Global.guide.guideTiles[_local3] as GuideTile);
                    _local5 = new Vector2(_local4.origin.x, _local4.origin.y);
                    if (((m_highlightedPoint.equals(_local5)) && (m_positionValid))){
                        m_selectedObject.setPosition(_local5.x, _local5.y);
                        m_selectedObject.conditionallyReattach();
                        m_selectedObject.onObjectDrop(m_objectStartPos);
                        m_selectedObject.setId(this.m_resource.getId());
                        m_objectPlacedSuccess = true;
                        this.completePlaceResource();
                        Global.guide.guideTiles.splice(_local3, 1);
                        _local4.cleanup();
                        _local4 = null;
                        break;
                    };
                    _local3++;
                };
            } else {
                if (((((_local2) && (Global.isVisiting()))) && ((_local2 == m_selectedLotSite)))){
                    _local6 = _local2.getPosition();
                    m_selectedObject.setPosition(_local6.x, _local6.y);
                    m_selectedObject.conditionallyReattach();
                    m_selectedObject.onObjectDrop(m_objectStartPos);
                    m_selectedObject.setId(this.m_resource.getId());
                    m_objectPlacedSuccess = true;
                    Global.franchiseManager.placementMode = false;
                    Global.world.popGameMode();
                    this.completePlaceResource(_local2);
                } else {
                    if (((_local2) && (!(Global.isVisiting())))){
                        return;
                    };
                    super.handleObjectDrop(_arg1);
                };
            };
        }
        override protected function onObjectDropCompleted():void{
            var _local1:MapResource;
            if (m_objectPlacedSuccess){
                this.completePlaceResource();
                if ((m_selectedObject is MapResource)){
                    _local1 = (m_selectedObject as MapResource);
                    _local1.onBuildingConstructionCompleted_PreServerUpdate();
                    _local1.startTime = GlobalEngine.serverTime;
                    if (((this.m_resource.isRoadVerifiable) && (!((m_selectedObject as MapResource).isAdjacentToAnyRoad)))){
                        Global.ui.showTickerMessage(this.m_needRoadTickerMessage);
                    };
                    _local1.trackAction(TrackedActionType.PLACE);
                };
            } else {
                super.onObjectDropCompleted();
            };
        }
        protected function completePlaceResource(_arg1:LotSite=null):void{
            var _local4:MapResource;
            var _local5:OwnedFranchiseData;
            var _local6:FranchiseExpansionData;
            var _local7:GameMode;
            var _local2:Item = Global.gameSettings.getItemByName(this.m_item);
            this.m_resource.setPosition(m_selectedObject.getPosition().x, m_selectedObject.getPosition().y, m_selectedObject.getPosition().z);
            this.m_resource.setDirection(m_selectedObject.getDirection());
            if (m_canPlaceToFriendHome){
                if (!m_isNearRoad){
                    return;
                };
                m_selectedObject.setId(this.m_resource.getId());
                (m_selectedObject as HarvestableResource).setState(HarvestableResource.STATE_PLANTED);
                Global.peddlerManager.displayCreateOutcryDialog(this.m_resource, this.itemData);
            } else {
                if (!_arg1){
                    if (m_isGift){
                        Global.player.inventory.removeItems(this.m_item, 1);
                    } else {
                        _local4 = (this.m_constructionSite) ? this.m_constructionSite : this.m_resource;
                        if (_local2.cost > 0){
                            Global.player.gold = (Global.player.gold - _local2.cost);
                            _local4.displayStatus(("-" + ZLoc.t("Main", "SpendCoins", {coins:_local2.cost})));
                        } else {
                            if (_local2.cash > 0){
                                Global.player.cash = (Global.player.cash - _local2.GetItemSalePrice());
                                _local4.displayStatus(("-" + ZLoc.t("Main", "SpendCash", {cash:_local2.GetItemSalePrice()})));
                            } else {
                                GlobalEngine.msg((_local2.name + " cost nothing, make sure this is intentional behavior."));
                            };
                        };
                        if (_local2.plantXp > 0){
                            Global.player.xp = (Global.player.xp + _local2.plantXp);
                        };
                    };
                    GameTransactionManager.addTransaction(new TPlaceMapResource(this.m_resource, m_isGift));
                    m_selectedObject.setId(this.m_resource.getId());
                } else {
                    if (!Global.world.isOwnerCitySam){
                        Global.world.citySim.lotManager.placeOrder(Global.world.ownerId, _arg1.getId(), this.m_item);
                        m_selectedObject.detach();
                        m_selectedObject.cleanup();
                        FranchiseViralManager.triggerFranchiseViralFeeds(FranchiseViralManager.VIRAL_APPROVEBUILDINGREQUEST, this.itemName);
                    } else {
                        GameTransactionManager.addTransaction(new TReplaceUserResource(this.m_item, _arg1));
                        Global.world.citySim.lotManager.replaceLotSite(_arg1.getId(), this.m_item, "", Global.player.uid);
                        m_selectedObject.detach();
                        m_selectedObject.cleanup();
                        _local5 = Global.franchiseManager.model.getOwnedFranchise(this.m_item);
                        if (_local5){
                            _local6 = FranchiseExpansionData.loadFirstFranchiseOnCitySamMap(this.m_item);
                            _local5.addLocation(_local6);
                        };
                    };
                };
            };
            var _local3:Boolean;
            if (_local2.multiplace){
                if ((((Global.player.gold < _local2.cost)) || ((Global.player.cash < _local2.cash)))){
                    _local3 = true;
                };
                if (((m_isGift) && ((Global.player.inventory.getItemCount(_local2) <= 0)))){
                    _local3 = true;
                };
            } else {
                _local3 = true;
            };
            if (_local3){
                if (Global.isVisiting()){
                    _local7 = new GMVisit(Global.world.ownerId);
                } else {
                    _local7 = new GMPlay();
                };
                Global.world.addGameMode(_local7);
            } else {
                Global.world.addGameMode(new GMPlaceMapResource(this.m_item, this.m_itemClass, m_isGift));
            };
            if (this.m_constructionSite){
                m_selectedObject.removeChild(this.m_resource);
                this.m_resource.detach();
                this.m_resource.cleanUp();
                m_selectedObject.setVisible(true);
            };
        }
        public function get displayedMapResource():MapResource{
            return (this.m_resource);
        }
        public function set itemData(_arg1:Object):void{
            this.m_itemData = _arg1;
        }
        public function get itemData():Object{
            return (this.m_itemData);
        }

    }
}//package GameMode 
